package uk.ac.cam.echo.app;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import uk.ac.cam.echo.messages.CheckInMessage;
import uk.ac.cam.echo.messages.UserMessage;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author Thom Sutcliffe
 */

/*
 * TODO: Add documentation
 */
public class FriendsActivity extends SecondaryActivity
{

	private List<String> friendAll = new LinkedList<String>();
	private List<String> friendAch = new LinkedList<String>();

	private List<CheckInMessage> friendCheckIn = new LinkedList<CheckInMessage>();
	private List<UserMessage> friendRequests;
	private List<UserMessage> friendList = new LinkedList<UserMessage>();

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friends);

		Button homeButton = (Button) findViewById(R.id.friendsHomeButton);
		Button showAllButton = (Button) findViewById(R.id.friendsall);
		Button showCheckInButton = (Button) findViewById(R.id.friendscheckin);
		Button showAchButton = (Button) findViewById(R.id.friendsach);
		Button requestsButton = (Button) findViewById(R.id.friendsreq);
		Button friendButton = (Button) findViewById(R.id.friendsrefresh);
		ListView listView = (ListView) findViewById(R.id.friendsList);
		TextView tv = (TextView) findViewById(R.id.friendstv);

		List<String> friendListString = new LinkedList<String>();
		List<String> friendRequestsString = new LinkedList<String>();
		List<String> friendCheckInString = new LinkedList<String>();

		ArrayAdapter<String> allaa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, friendAll);
		ArrayAdapter<String> achievementAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, friendAch);

		ArrayAdapter<String> checkInAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friendCheckInString);
		ArrayAdapter<String> requestAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friendRequestsString);
		ArrayAdapter<String> friendAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friendListString);

		Bundle extras = getIntent().getExtras();
		final String option;
		final String title;

		if (extras == null)
		{
			option = "friends";
			title = "Friend List";
		}
		else
		{
			option = extras.getString("option");
			title = extras.getString("title");
		}

		tv.setText(title);

		try
		{
			if (option.equals("checkin"))
			{
				String friendRFID;
				if ((friendRFID = extras.getString("friendRFID")) != null)
					friendCheckIn = DBInt.getInstance().getFriendCheckIns(friendRFID);
				else
					friendCheckIn = DBInt.getInstance().getFriendCheckIns();

				for (CheckInMessage c : friendCheckIn)
				{
					friendCheckInString.add(c.toString());
				}
				checkInAdapter.notifyDataSetChanged();
				listView.setAdapter(checkInAdapter);
			}
			else if (option.equals("ach"))
			{
				listView.setAdapter(achievementAdapter);
			}
			else if (option.equals("req"))
			{
				friendRequests = DBInt.getInstance().getFriendRequests();
				friendRequestsString.clear();

				for (UserMessage u : friendRequests)
				{
					friendRequestsString.add(u.toString());
				}

				requestAdapter.notifyDataSetChanged();
				listView.setAdapter(requestAdapter);

			}
			else if (option.equals("friends"))
			{
				friendList = DBInt.getInstance().getFriendList();
				friendListString.clear();

				for (UserMessage u : friendList)
				{
					friendListString.add(u.toString());
				}

				friendAdapter.notifyDataSetChanged();
				listView.setAdapter(friendAdapter);

			}
			else
			{
				listView.setAdapter(allaa);
			}
		}
		catch (Exception e)
		{
			showOKMessage("Could not connect to the database.\n" + e);
		}

		homeButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				try
				{
					Intent i = new Intent(self, EchoActivity.class);
					self.startActivity(i);
					self.finish();
				}
				catch (Exception e)
				{}
			}
		});

		friendButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, FriendsActivity.class);
				i.putExtra("option", "friends");
				i.putExtra("title", "Friend List");
				self.startActivity(i);
				self.finish();
			}
		});

		showAllButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, FriendsActivity.class);
				i.putExtra("option", "all");
				self.startActivity(i);
				self.finish();
			}
		});

		showCheckInButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, FriendsActivity.class);
				i.putExtra("option", "checkin");
				i.putExtra("title", "Friend Check Ins");
				self.startActivity(i);
				self.finish();
			}
		});

		showAchButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, FriendsActivity.class);
				i.putExtra("option", "ach");
				i.putExtra("title", "Friend Achievements");
				self.startActivity(i);
				self.finish();
			}
		});

		requestsButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, FriendsActivity.class);
				i.putExtra("option", "req");
				i.putExtra("title", "Friend Requests");
				self.startActivity(i);
				self.finish();
			}
		});

		// TODO: Consider rejecting requests
		listView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				if (option.equals("req"))
				{
					try
					{
						UserMessage requester = friendRequests.get(position);

						DBInt.getInstance().addFriend(requester.getRFID());

					}
					catch (IOException e)
					{
						showOKMessage("Could not connect to the database.\n" + e);
					}
				}
				else if (option.equals("friends"))
				{
					UserMessage friend = friendList.get(position);

					Intent i = new Intent(self, FriendsActivity.class);
					i.putExtra("option", "checkin");
					i.putExtra("title", friend.toString() + " Check Ins");
					i.putExtra("friendRFID", friend.getRFID());
					self.startActivity(i);
					self.finish();
				}
			}
		});
	}

}
